package com.project.frba.bi;

import com.project.frba.bi.descriptor.DatabaseDescriptor;
import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.extraction.database.DatabaseExtractor;
import com.project.frba.bi.extraction.database.QueryBuilder;
import com.project.frba.bi.loading.Loader;
import com.project.frba.bi.loading.LoaderImpl;
import com.project.frba.bi.transformation.TransformationResult;

public class ETLThread implements Runnable {

	private Descriptor descriptor;
	
	public ETLThread(Descriptor descriptor){
		this.descriptor = descriptor;
	}
	
	@Override
	public void run() {
		try {
			Long start = BiUtil.getActualSeconds();
			System.out.println("Procesando descriptor: " + descriptor.getDescriptorName());
			
			QueryBuilder queryBuilder = new QueryBuilder((DatabaseDescriptor)descriptor);
			queryBuilder.buildQuery();
			
			//Extraction and Transformation
			DatabaseExtractor extractor = new DatabaseExtractor(queryBuilder.getQuery(), descriptor);
			TransformationResult tResult = extractor.getResult();
			
			//Loading
			Loader loader = new LoaderImpl();
			loader.load(tResult);
			Long end = BiUtil.getActualSeconds();
			System.out.println(" ..... Finalizado. Duracion total: " + (end-start) );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	


}
